package org.unicen.exa.io.worker.assigner.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.unicen.exa.io.worker.assigner.core.model.Worker;
import org.unicen.exa.io.worker.assigner.dao.WorkerDao;

@Repository("workerDao")
public class WorkerDaoImpl extends BaseDaoImpl<Worker> implements WorkerDao<Worker>{

	private Logger logger = Logger.getLogger(WorkerDaoImpl.class);

	private Map<Long, Worker> map = new HashMap<Long, Worker>();
	

	public void post(Long id, Worker model) {
//		logger.info(String.format("STORING Worker with id equal to %s", id.toString()));
		map.put(id, model);
	}

	public void put(Worker model) {
//		logger.info(String.format("MERGING Worker with id equal to %s", model.getId().toString()));
		map.put(model.getId(), model);
	}

	public void delete(Worker model) {
//		logger.info(String.format("REMOVING Worker with id equal to %s", model.getId().toString()));
		map.remove(model.getId());
	}

	public Worker get(Long id) {
//		logger.info(String.format("GETTING Worker with id equal to %s", id.toString()));
		return map.get(id);
	}

	public synchronized List<Worker> gets() {
		logger.info(String.format("GETTING all workers"));
		List<Worker> list = new ArrayList<Worker>();
		for (Entry<Long, Worker> iterable_element : map.entrySet()) {
			list.add(iterable_element.getValue());
		}
		
		return list;
	}

	public Integer countWorkersByState(String workerState) {
		logger.info(String.format("COUNTING workers state in %s", workerState));
		Integer count = new Integer(0);
		for (Entry<Long, Worker> iterable_element : map.entrySet()) {
			if (iterable_element.getValue().getState().equals(workerState)){
				count++;
			}
		}
		logger.info(String.format("Amount worker with state in %s equals %s", workerState, count.toString()));
		return count;
	}


}
